﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using app.Models;
using AmuTools;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace app.Api
{
    /*
        公用接口
    */
    public class ApiStatistics : ApiBase
    {
        public ApiStatistics(Controller ctx) : base(ctx) { }


        public void Do(string action)
        {
            CheckPermission(IDENTITY.ADMIN);
            switch (action)
            {
                case "oneMonth":
                    {
                        string date_unit = GetStringParam(new ReqStringParamOption { Name = "date_unit" });
                        // 特殊获取一下今天的数据存到数据库中
                        Statistic.SaveOneDayData(DateTime.Now);
                        // 根据时间单位、开始时间、结束时间得到数据
                        string group_by = "";
                        if (date_unit == "year") group_by = "[year]";
                        else if (date_unit == "month") group_by = "[year], [month]";
                        else if (date_unit == "date") group_by = "[year], [month], [day]";
                        string sql_str = string.Format(@"
                            select area, {0}, sum(member_count) member_count, sum(demand_count) demand_count, sum(service_count) service_count, sum(abutment_count) abutment_count
                            from t_statistic
                            where [date] > @begin_time and [date] < @end_time
                            group by area, {0}", group_by);
                        DataTable dt = G.DB.Get(sql_str, new SqlParameter[] {
                            G.DB.CreateSqlParameter("@begin_time", req_begin_time),
                            G.DB.CreateSqlParameter("@end_time", req_end_time)
                        }).FirstTable;
                        ResponseJson(new ObjectResult { data = dt });
                        return;
                    }
                case "monthCount":
                    {
                        int feedback_month = getMonthCount<Feedback>("status = 1");
                        int service_month = getMonthCount<ServiceItem>("status = 1");
                        int demand_month = getMonthCount<DemandItem>("status = 1");
                        int member_month = getMonthCount<Member>("");
                        int firm_month = getMonthCount<Member>("status = 1 and role_type = 4");
                        int service_register_month = getMonthCount<Member>("status = 1 and role_type = 5");
                        Dictionary<string, int> dictionary = new Dictionary<string, int>();
                        dictionary.Add("feedback_month", feedback_month);
                        dictionary.Add("service_month", service_month);
                        dictionary.Add("demand_month", demand_month);
                        dictionary.Add("member_month", member_month);
                        dictionary.Add("firm_month", firm_month);
                        dictionary.Add("service_register_month", service_register_month);
                        ResponseJson(new ObjectResult { data = dictionary });
                        return;
                    }
            }
        }
        //获取当月各项数据统计数量
        private int getMonthCount<T>(string condition)
        {
            long end_time_stamp = long.Parse(Api.DateTimeEx.GetNowTimeStamp());
            DateTime datetime = DateTime.Now.AddMonths(-1);
            long start_time_stamp = (long)((datetime - new DateTime(1970, 1, 1)).TotalMilliseconds);
            Cd cd = new Cd();
            cd.And("create_time < {0}", end_time_stamp);
            cd.And("create_time > {0}", start_time_stamp);
            cd.And(condition);
            int count = G.DB.GetCount<T>(cd.Render());
            return count;
        }

    }
}